package com.wang.tool.dagre;

import com.alibaba.fastjson.JSON;
import com.wang.tool.dagre.node.DagreNode;
import com.wang.tool.common.ErrorInfo;

/**
 * 节点执行情况
 *
 * @author shaopeng
 * @date 2022/12/4
 */
public class NodeState {

    /**
     * id
     */
    private String id;


    /**
     * 开启
     */
    private int begin = -1;

    /**
     * 结束
     */
    private int end = -1;

    /**
     * 异常
     */
    private ErrorInfo error;

    /**
     * 结果
     */
    private Object result;

    /**
     * 是否是慢调用
     * @see DagreNode#getSlowTime
     */
    private Boolean slow;


    /**
     * 是否被取消
     * @see DagreContext#tryCancelNode
     */
    private boolean cancel;

    /**
     * 是否被忽略执行
     * @see DagreNode#preCheck
     */
    private boolean ignore;

    /**
     * 结果快照
     * 节点返回时状态,可能多个节点返回值均是一个(仅修饰)值
     * 仅在debug场景下保留当时快照
     */
    private String resultSnapshot;


    /**
     * 有没有开始
     *
     * @return
     */
    public boolean judgeIsBegin() {
        return begin != -1;
    }

    /**
     * 有没有结束
     *
     * @return
     */
    public boolean judgeIsEnd() {
        return end != -1;
    }

    /**
     * 成功没
     *
     * @return
     */
    public boolean judgeIsSuccess() {
        return error == null;
    }

    public <T> T getResult(Class<T> type) {
        return (T) result;
    }

    public int getBegin() {
        return begin;
    }

    public void setBegin(int begin) {
        this.begin = begin;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public ErrorInfo getError() {
        return error;
    }

    public void setError(ErrorInfo error) {
        this.error = error;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getResultSnapshot() {
        return resultSnapshot;
    }

    public void setResultSnapshot(String resultSnapshot) {
        this.resultSnapshot = resultSnapshot;
    }

    public boolean isCancel() {
        return cancel;
    }

    public void setCancel(boolean cancel) {
        this.cancel = cancel;
    }

    public Boolean getSlow() {
        return slow;
    }

    public void setSlow(Boolean slow) {
        this.slow = slow;
    }

    public boolean isIgnore() {
        return ignore;
    }

    public void setIgnore(boolean ignore) {
        this.ignore = ignore;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
